version: "3.9"

services:

  postgres:
    container_name: ${POSTGRES_SERVICE_NAME}
    image: postgres:14
    env_file:
      - .env
    restart: always
    ports:
      - ${POSTGRES_PORT}:5432
    volumes:
      - ./tmp/pgdata:/var/lib/postgresql/data
    environment:
      PGDATA: /var/lib/postgresql/data/pgdata
      POSTGRES_HOST_AUTH_METHOD: trust
    healthcheck:
      test:
        [
          'CMD-SHELL',
          'pg_isready -d $${POSTGRES_DB} -U $${POSTGRES_USER}'
        ]
      interval: 10s
      timeout: 5s
      retries: 5
    networks:
      - local

  pgadmin:
    image: dpage/pgadmin4:4.18
    restart: always
    env_file:
      - .env
    ports:
      - "${PGADMIN_PORT}:${PGADMIN_LISTEN_PORT}"
    user: '$UID:$GID'
    volumes:
      - ./tmp/pgadmin:/var/lib/pgadmin
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - local

  server:
    env_file:
      - .env
    build:
      context: .
      dockerfile: Dockerfile.server
      args:
        SERVER_PORT: ${SERVER_PORT}
    restart: always
    ports:
      - "${SERVER_PORT}:${SERVER_PORT}"
    depends_on:
      postgres:
        condition: service_healthy
    networks:
      - local

  client:
    env_file:
      - .env
    build:
      context: .
      dockerfile: Dockerfile.client
    restart: always
    ports:
      - "${CLIENT_PORT}:80"
    networks:
      - local

networks:
  local:
    driver: bridge
